package unpackaged;

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.avro.Protocol;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.ipc.NettyTransceiver;
import org.apache.avro.ipc.generic.GenericRequestor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import tt.dz.entity.charger.OmsChargerBuildApply;
@Component
public class OmsAppServerClient 
{
	@Value(value="${rpc.self.port}")
	private int port;
	private String ip= "localhost";
	
	private Protocol protocol;
	
//	@PostConstruct
	public void init()
	{
    	try 
    	{
			String filePath = this.getClass().getResource("omsAvro.json").getPath();
			protocol = Protocol.parse(new File(filePath));
		} 
    	catch (IOException e) 
    	{
			e.printStackTrace();
		}
    }
	
	public void asInteface(){
		init();
    	NettyTransceiver transceiver = null;
    	OmsChargerBuildApply obj = new OmsChargerBuildApply();
    	obj.setUser_id("123245678");
    	obj.setBuild_addr("香年广场T2 1906");
    	obj.setBuild_count(20);
    	obj.setBuild_type(1);
    	obj.setReal_name("boss");
    	obj.setTelephone("18615610512");
   
		try 
		{
			transceiver = new NettyTransceiver(new InetSocketAddress(ip, port));
			GenericRequestor requestor = new GenericRequestor(protocol, transceiver);
			GenericRecord requestData = new GenericData.Record(protocol.getMessages().get("addBuidChargerApply").getRequest());  
			requestData.put("applyDatas", JSON.toJSONStringWithDateFormat(obj, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat));
	        Object result = requestor.request("addBuidChargerApply", requestData);
	        System.out.println(result);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		} 
		finally 
		{
			if(transceiver != null)
			{
				transceiver.close();
			}
		}
    }
	
	public void asFindByUserIdInteface(){
		init();
    	NettyTransceiver transceiver = null;
		try 
		{
			transceiver = new NettyTransceiver(new InetSocketAddress(ip, port));
			GenericRequestor requestor = new GenericRequestor(protocol, transceiver);
			GenericRecord requestData = new GenericData.Record(protocol.getMessages().get("findBuidChargerApplyByUserId").getRequest());  
			requestData.put("userId", "123245678");
	        Object result = requestor.request("findBuidChargerApplyByUserId", requestData);
	        System.out.println(result);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		} 
		finally 
		{
			if(transceiver != null)
			{
				transceiver.close();
			}
		}
    }
}
